package cn.itcast.wushangxiantu.Activity;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.Fragment.jiaoyisuo.JiaoYiSuoDaoju;
import cn.itcast.wushangxiantu.Fragment.jiaoyisuo.JiaoYiSuoShangjia;
import cn.itcast.wushangxiantu.Fragment.jiaoyisuo.JiaoYiSuoZhuangBei;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class JiaoYiSuo extends AppCompatActivity implements View.OnClickListener {

    private TextView tvZhuangbei;
    private TextView tvShangJia;
    private TextView tvLingshi;
    private FragmentTransaction fragmentTransaction;
    int number = 0;//代表当前第几个
    private TextView tvGm;
    private TextView tvSj;
    private TextView tvShuaXin;
    private TextView tvMoney;
    private View viewZhuangbei;
    private View viewShangJia;
    private View viewLingshi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiaoyisuo);
        initView();
        getJiaoYi();
    }

    private void initView() {
        tvZhuangbei = (TextView) findViewById(R.id.tvZhuangbei);
        tvShangJia = (TextView) findViewById(R.id.tvShangJia);
        tvLingshi = (TextView) findViewById(R.id.tvLingshi);

        tvZhuangbei.setOnClickListener(this);
        tvShangJia.setOnClickListener(this);
        tvLingshi.setOnClickListener(this);

        fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fl, new JiaoYiSuoZhuangBei(this));
        fragmentTransaction.commit();
        tvGm = (TextView) findViewById(R.id.tvGm);
        tvSj = (TextView) findViewById(R.id.tvSj);
        tvShuaXin = (TextView) findViewById(R.id.tvShuaXin);
        tvShuaXin.setOnClickListener(this);
        tvMoney = (TextView) findViewById(R.id.tvMoney);
        viewZhuangbei = (View) findViewById(R.id.viewZhuangbei);
        viewShangJia = (View) findViewById(R.id.viewShangJia);
        viewLingshi = (View) findViewById(R.id.viewLingshi);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvZhuangbei:
                viewZhuangbei.setVisibility(View.VISIBLE);
                viewLingshi.setVisibility(View.INVISIBLE);
                viewShangJia.setVisibility(View.INVISIBLE);
                if (number != 0) {
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fl, new JiaoYiSuoZhuangBei(this));
                    fragmentTransaction.commit();
                }
                number = 0;
                break;
            case R.id.tvShangJia:
                viewZhuangbei.setVisibility(View.INVISIBLE);
                viewLingshi.setVisibility(View.INVISIBLE);
                viewShangJia.setVisibility(View.VISIBLE);
                if (number != 1) {
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fl, new JiaoYiSuoShangjia(this));
                    fragmentTransaction.commit();
                }
                number = 1;
                break;
            case R.id.tvLingshi:
                viewZhuangbei.setVisibility(View.INVISIBLE);
                viewLingshi.setVisibility(View.VISIBLE);
                viewShangJia.setVisibility(View.INVISIBLE);
                if (number != 2) {
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fl, new JiaoYiSuoDaoju(this));
                    fragmentTransaction.commit();
                }
                number = 2;
                break;
            case R.id.tvShuaXin:
                getJiaoYi();
                break;
        }
    }

    private void getJiaoYi() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=maimaiyulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("maimai1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("maimai2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {
                    user = null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    User user = (User) message.obj;
                    if (null != user) {
                        tvGm.setText("购买凭证：" + user.getDj());
                        tvSj.setText("上架凭证：" + user.getJy());
                        tvMoney.setText("持有金币：" + user.getMoney());
                    } else {
                        ToastUtil.toast(JiaoYiSuo.this, "服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });
}
